﻿namespace MPIS

open System
open System.ServiceProcess
open System.ServiceModel
open System.ServiceModel.Description

type public ISService() =
    inherit ServiceBase()

    let mutable components:System.ComponentModel.IContainer = null
    let mutable eventLog:System.Diagnostics.EventLog = null
    let mutable stop = false
    let baseAddress = new Uri("http://localhost:8080/MPIS")
    let host = new ServiceHost(typeof<ISPackageService.Services.ISPackageService>, baseAddress)
    let mutable smb = new ServiceMetadataBehavior()

    do
        eventLog <- new System.Diagnostics.EventLog()
        (eventLog :> System.ComponentModel.ISupportInitialize).BeginInit()
        eventLog.Log <- "Application";
        eventLog.Source <- "ISPackage_WinService";
        base.ServiceName <- "MPIS";
        (eventLog :> System.ComponentModel.ISupportInitialize).EndInit()

    member public this.stopping
        with get () = stop
        and set (value) = stop <- value

    override this.OnStart((args: string array)) =
        
        smb.HttpGetEnabled <- true
        smb.MetadataExporter.PolicyVersion <- PolicyVersion.Policy15
        host.Description.Behaviors.Add(smb)

        host.Open();

        Console.ReadLine() |> ignore
                
    override this.OnStop() =
        base.OnStop()
        this.stopping <- true
        host.Close()
    
    override this.Dispose(disposing:bool) =
        match disposing && (components <> null) with
        | true ->
            components.Dispose()
            base.Dispose(disposing)
        | _ ->
            ()        

    member public this.start() =
        this.OnStart([| "" |]) |> ignore
    

open System.ComponentModel
open System.Configuration.Install

[<RunInstaller(true)>]
type public ProjectInstaller() =
    inherit Installer()
    let mutable components:System.ComponentModel.IContainer = null
    let mutable serviceProcessInstaller:System.ServiceProcess.ServiceProcessInstaller = null
    let mutable serviceInstaller:System.ServiceProcess.ServiceInstaller = null

    do
        serviceProcessInstaller <- new System.ServiceProcess.ServiceProcessInstaller()
        serviceInstaller <- new System.ServiceProcess.ServiceInstaller()
        serviceProcessInstaller.Account <- System.ServiceProcess.ServiceAccount.LocalService;
        serviceProcessInstaller.Password <- null;
        serviceProcessInstaller.Username <- null;
        serviceInstaller.Description <- "MPIS ISPackageService"
        serviceInstaller.DisplayName <- "MPIS ISPackageService"
        serviceInstaller.ServiceName <- "ISPackageService"
        base.Installers.AddRange([| serviceProcessInstaller :> Installer; serviceInstaller :> Installer |])
    
    

    override this.Dispose(disposing:bool) = 
        match disposing && (components <> null) with
        | true ->
            components.Dispose()
            base.Dispose(disposing)
        | _ ->
            ()


